package com.selen.mapper;

import com.selen.dto.PageDTO;
import com.selen.entities.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Selen
 */
@Repository
public interface SchoolMapper {

    @Insert("insert into ums_school " +
            "(`title`, `college`, `major`, `clazz`, `address`, `info`, `deleted`, `created`, `updated`) " +
            "values (#{title}, #{college}, #{major}, #{clazz}, #{address}, #{info}, #{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(School school);

    @Select("select us.id, us.title, us.college, us.major, us.clazz, us.address, " +
            "us.info, us.deleted, us.created, us.updated " +
            "from ums_school as us " +
            "where us.id = #{param1} and us.deleted = 0")
    School selectById(Long id);

    @Update("<script>" +
            "update ums_school " +
            "<set> " +
            "<if test = 'title != null'> title = #{title}, </if> " +
            "<if test = 'college != null'> college = #{college}, </if> " +
            "<if test = 'major != null'> major = #{major}, </if> " +
            "<if test = 'clazz != null'> clazz = #{clazz}, </if> " +
            "<if test = 'address != null'> address = #{address}, </if> " +
            "<if test = 'info != null'> info = #{info}, </if> " +
            "<if test = 'deleted != null'> deleted = #{deleted}, </if> " +
            "<if test = 'created != null'> created = #{created}, </if> " +
            "<if test = 'updated != null'> updated = #{updated}, </if> " +
            " </set> " +
            "where id = #{id} and deleted = 0 " +
            "</script>")
    int updateById(School school);

    @Update("update ums_school set deleted = 1, updated = now() " +
            "where id = #{param1}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update ums_school set deleted = 1, updated = now() " +
            "where " +
            "<foreach collection = 'list' item = 'e' open = 'id in (' close = ')' separator = ',' > " +
            "${e}" +
            " </foreach> " +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select us.id, us.title, us.college, us.major, us.clazz, " +
            "us.address, us.info, us.deleted, us.created, us.updated " +
            "from ums_school as us " +
            "where us.deleted = 0 ")
    List<School> selectAll();

    @Delete("delete from ums_school where id = #{param1}")
    int deleteById(Long id);


}
